package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.Specification;
import com.shaoshaohuo.app.utils.ViewUtil;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 展示规格  gridview 适配器
 */
public class SpecificationAdapter extends BaseAdapter {

    private Context context;
    private GridView gridView;
    private LinkedList<String[]> list= new LinkedList<String[]>();

    public SpecificationAdapter(Context context, GridView gridView, Map<Specification, List<Specification>> data) {
        this.context = context;
        this.gridView = gridView;
        String[] kvArr = null;
        for (Specification key : data.keySet()) {
            kvArr = new String[2];
            String str = key.specification_name + ": ";
            kvArr[0] = str;
            List<Specification> values = data.get(key);
            StringBuilder sb = new StringBuilder();
            for (Specification value : values) {
                sb.append(value.specification_val + "，");
            }
            if (sb.length() > 0) {
                kvArr[1] = sb.substring(0, sb.length() - 1);
            } else {
                kvArr[1] = "";
            }
            list.add(kvArr);
        }

        if (list.size() % 2 != 0) {
            kvArr = new String[2];
            kvArr[0] = "";
            kvArr[1] = "";
            list.add(kvArr);
        }

        Collections.sort(list, new Comparator<String[]>() {
            @Override
            public int compare(String[] lhs, String[] rhs) {
                String leftStr = lhs[0] + lhs[1];
                String rightStr = rhs[0] + rhs[1];
                if (leftStr.length() < rightStr.length()) {
                    return 1;
                }
                return -1;
            }
        });

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public String[] getItem(int position) {

        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final View view = View.inflate(context, R.layout.item_gridview_specifications, null);
        final TextView tv_key = (TextView) view.findViewById(R.id.tv_key);
        final TextView tv_value = (TextView) view.findViewById(R.id.tv_value);

        tv_key.setText(getItem(position)[0]);
        tv_value.setText(getItem(position)[1]);

        if (position == list.size() - 1) {
            setLayoutListener();
        }


        return view;
    }

    private void setLayoutListener() {
        gridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    gridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }

                int parentHeight = ViewUtil.dip2px(context, 1.0f);

                for (int i = 1; i < gridView.getChildCount(); i += 2) {
                    View firstView = gridView.getChildAt(i - 1);
                    View lastView = gridView.getChildAt(i);

                    int firstHeight = firstView.getMeasuredHeight();
                    int lastHeight = lastView.getMeasuredHeight();

                    int height = firstHeight > lastHeight ? firstHeight : lastHeight;
                    parentHeight = parentHeight + height + ViewUtil.dip2px(context, 1.0f);

                    Log.e("height = ", height + "");

                    firstView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, height));
                    lastView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, height));

                }
                Log.e("parentHeight  = ", parentHeight + "");
                gridView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, parentHeight));
                ((FrameLayout)gridView.getParent()).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, parentHeight));
            }
        });
    }


}
